<?php

namespace Yearnfar\Bundle\CmsBundle\Controller;

use Doctrine\ORM\Tools\Pagination\Paginator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Yearnfar\Bundle\CmsBundle\Entity\admin;

/**
 * @Route("/user")
 * @Template()
 */
class UserController extends BaseController
{
    /**
     * @Route("/")
     */
    public function indexAction()
    {
        $request = $this->get('request');

        $rows = max(1, $request->get('rows', 10));
        $page = max(1, $request->get('page', 1));

        $em = $this->getDoctrine()->getManager();
        $cq = $em
            ->createQuery("SELECT u FROM CmsBundle:admin u")
            ->setFirstResult(($page-1) * $rows)
            ->setMaxResults($rows);

        $records = count(new Paginator($cq, true));
        $user    = $cq->getArrayResult();

        $_group = [];
        $groups = $em->getRepository('CmsBundle:groups')->findAll();
        foreach($groups as $og) {
            $_group[$og->getRole()] = $og->getName();
        }
        //分组名称
        foreach($user as $k => $au) {
            if (isset($_group[$au['roles'][0]])) {
                $user[$k]['roles'][0] = $_group[$au['roles'][0]];
            }
        }

        return [
            'records'=> $records,
            'user'   => $user,
            'page'   => $page,
            'total'  => ceil($records/$rows),
        ];
    }

    /**
     * @Route("/edit")
     */
    public function editAction()
    {
        $request = $this->get('request');
        $submit  = $request->get('submit');
        $uid     = $request->get('uid', 0);

        $em = $this->getDoctrine()->getManager();
        $current = $uid>0 ? $em->find('CmsBundle:admin', $uid) : new admin();
        $groups  = $em->getRepository('CmsBundle:groups')->findAll();

        if ($submit) {
            $username = $request->get('username');
            $email    = $request->get('email');
            $password = $request->get('password', '');
            $role     = $request->get('role');
            $avatar   = $request->get('avatar', '');

            $_password = $current->getPassword();
            if ($_password && $password=='') {
                $password = $_password;
            } else {
                $password = $this->get('security.password_encoder')->encodePassword($current, $password);
            }

            $privilege = (int)$request->get('privilege');

            $current
                ->setUsername($username)
                ->setEmail($email)
                ->setPassword($password)
                ->setRoles([$role])
                ->setAvatar($avatar)
                ->setPrivilege([]);

            $em->persist($current);
            $em->flush();

            if ($uid>0) {
                return $this->msgOut('修改用户信息成功！', '/cms/user/');
            }

            return $this->msgOut('添加用户成功！', '/cms/user/');
        }

        return [
            'groups'  => $groups,
            'current' => $current,
        ];
    }

    /**
     * @Route("/delete")
     */
    public function deleteAction()
    {
        return array(
            // ...
        );    }

    /**
     * @Route("/profile")
     */
    public function profileAction()
    {
        return array(
                // ...
            );    }

    /**
     * @Route("/ibox")
     */
    public function iboxAction()
    {
        return array(
                // ...
            );    }

}
